﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;

namespace eVoting
{
    public partial class Vote : System.Web.UI.Page
    {
        DBManager db;
        int selectedPollID;
        int selectedCandidateID;
        int userID = 3; //provided by maged

        protected void Page_Load(object sender, EventArgs e)
        {
            db = new DBManager();
            messageLabel.Visible = false;
            CandidateList.Items.Clear();
            
           
            if (!IsPostBack)
            {
                List<Poll> polls = db.getAssignedPollsByUserID(userID);
                if (polls.Count == 0)
                {
                    messageLabel.Text = "Sorry, You are Not Allowed to Vote. Please, Contact the System Administrator!";
                    messageLabel.Visible = true;
                    PollList.Enabled = false;
                }
                PollList.Items.Add("Select a poll");
                
                
                for (int i = 0; i < polls.Count; i++)
                {
                    PollList.Items.Add(new ListItem(polls[i].name, polls[i].ID + ""));

                }


            }
         
        }

        protected void PollList_SelectedIndexChanged(object sender, EventArgs e)
        {
            db = new DBManager();
            if (PollList.SelectedIndex != 0)
            { 
                selectedPollID = int.Parse(PollList.SelectedItem.Value);
                if (db.hadVoted(userID, selectedPollID) == false)
                {
                    pollCandidates();
                }
                else
                {
                    messageLabel.Visible = true;
                }
            }
            List<PollUser> pollUsersVoted = db.getUsersVotedInPoll(userID, selectedPollID);
            for(int i=0; i<pollUsersVoted.Count;i++)
            {
                int uID= pollUsersVoted[i].user;
                User userMatched = db.getUserById(uID);
                TableRow row = new TableRow();
                TableCell cell1 = new TableCell();
                cell1.Text = userMatched.firstName +" "+ userMatched.lastName;
                row.Cells.Add(cell1);
                TableCell cell2 = new TableCell();
                cell2.Text = pollUsersVoted[i].candidate+"";
                row.Cells.Add(cell2);
                Table1.Rows.Add(row);

               
            }
            
        }

        protected void pollCandidates()
        {
            db = new DBManager();
            List<Candidate> candidates = db.getCandidatesByPoll(selectedPollID);
            for(int i=0; i < candidates.Count; i++)
            {
                CandidateList.Items.Add(new ListItem(candidates[i].firstName +" "+ candidates[i].lastName, candidates[i].ID+""));

            }
        }

        protected void CandidateList_SelectedIndexChanged(object sender, EventArgs e)
        {
            selectedCandidateID = int.Parse(CandidateList.SelectedItem.Value);
            //Otherwise value resets to 0 !!!
            selectedPollID = int.Parse(PollList.SelectedItem.Value); 
        }

        protected void submitCandidate()
        {
            db = new DBManager();
            bool inserted = db.insertPollUser(userID, selectedPollID, selectedCandidateID);
        }

        protected void candidateButton_Click(object sender, EventArgs e)
        {
            submitCandidate();
        }
    }
}
